// api.js
import stroage from '@/utils/storage.js'
const baseUrl = 'http://localhost:8000'; // 修改为你的API地址

export default {
  request(options) {
    return new Promise((resolve, reject) => {
		const token = stroage.get('token');
		uni.showLoading({
			title: '加载中...',
			mask: options.loading  || true
		});
      uni.request({
        url: baseUrl + options.url,
        method: options.method || 'GET',
        data: options.data || {},
        header: {
          'content-type': 'application/json', // 默认值
		  'Authorization': `Bearer ${token}`, 
          ...options.header
        },
        success(res) {
			uni.hideLoading()
          if (res.statusCode === 200) {
            resolve(res);
          } else {
            reject(res);
          }
        },
        fail(err) {
          reject(err);
        }
      });
    });
  },
  get(url, data, options = {}) {
    return this.request({
      url,
      data,
      method: 'GET',
      ...options
    });
  },
  post(url, data, options = {}) {
	  console.log('data',data)
    return this.request({
      url,
      data,
      method: 'POST',
      ...options
    });
  },
  put(url, data, options = {}) {
    return this.request({
      url,
      data,
      method: 'PUT',
      ...options
    });
  },
  delete(url, data, options = {}) {
    return this.request({
      url,
      data,
      method: 'DELETE',
      ...options
    });
  },
  patch(url, data, options = {}) {
      return this.request({
        url,
        data,
        method: 'PATCH',
        ...options
      });
    }
}
